package com.tanhua.server.controller;

import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.domain.vo.ErrorResult;
import com.tanhua.server.service.CommentService;
import com.tanhua.server.service.MovementsMQService;
import com.tanhua.server.service.MovementsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("movements")
@Slf4j
public class MovementsController {

    @Autowired
    private MovementsService movementsService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private MovementsMQService movementsMQService;

    /**
     * 接口名称：动态-发布
     * 接口路径：POST/movements
     * 需求分析：发布动态，需要往动态表、自己的相册表、好友的时间线表存储发布动态的内容
     */
    @PostMapping
    public ResponseEntity<Object> saveMovements(Publish publish, MultipartFile[] imageContent) throws IOException {
        log.info("接口名称：动态-发布");
        boolean flag = movementsMQService.findFreeze(publish);
        if(flag){
            return ResponseEntity.ok(ErrorResult.error());
        }
        ResponseEntity<Object> entity = movementsService.saveMovements(publish, imageContent);
        // 发送mq消息
        movementsMQService.publishMsg(publish.getId().toString());
        return entity;
    }

    /**
     * 接口名称：好友动态
     * 接口路径：GET/movements
     * 需求分析：查询好友动态，就是查看“朋友圈”。在自己的时间线表中存储了所有好友的动态
     * 举例：登陆用户id是1，查询好友动态就是查询 db.quanzi_time_line_1.find()
     */
    @GetMapping
    public ResponseEntity<Object> queryPublishList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pagesize){
        if (page<1) {
            page = 1; // 解决前端模拟器传入页码异常问题
        }
        return movementsService.queryPublishList(page,pagesize);
    }

    /**
     * 接口名称：推荐动态
     * 接口路径：GET/movements/recommend
     * 查询的表：recommend_quanzi
     */
    @GetMapping("recommend")
    public ResponseEntity<Object> queryRecommendPublishList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pagesize){
        if (page<1) {
            page = 1; // 解决前端模拟器传入页码异常问题
        }
        return movementsService.queryRecommendPublishList(page,pagesize);
    }






    /**
     * 接口名称：动态-点赞
     * 接口路径：GET/movements/:id/like
     * 需求描述：往quanzi_comment评论表插入数据并更新动态表中点赞数量，且返回总点赞数
     */
    @GetMapping("{id}/like")
    public ResponseEntity<Object> likeComment(@PathVariable("id") String publishId){
        log.info("接口名称：动态-点赞");
        ResponseEntity<Object> entity = commentService.likeComment(publishId);
        // 发送mq消息
        movementsMQService.likePublishMsg(publishId);
        return entity;
    }

    /**
     * 接口名称：动态-取消点赞
     * 接口路径：GET/movements/:id/dislike
     * 需求分析：删除评论表中点赞数据、修改动态表点赞总数减1、返回点赞数量
     */
    @GetMapping("{id}/dislike")
    public ResponseEntity<Object> disLikeComment(@PathVariable("id") String publishId){
        log.info("接口名称：动态-取消点赞");
        movementsMQService.disLikePublishMsg(publishId);
        return commentService.disLikeComment(publishId);
    }

    /**
     * 接口名称：动态-喜欢
     * 接口路径：GET/movements/:id/love
     */
    @GetMapping("{id}/love")
    public ResponseEntity<Object> loveComment(@PathVariable("id") String publishId){
        log.info("接口名称：动态-喜欢");
        movementsMQService.lovePublishMsg(publishId);
        return commentService.loveComment(publishId);
    }

    /**
     * 接口名称：动态-取消喜欢
     * 接口路径：GET/movements/:id/unlove
     * 需求描述：删除评论数据、修改动态表、返回喜欢总数
     */
    @GetMapping("{id}/unlove")
    public ResponseEntity<Object> unloveComment(@PathVariable("id") String publishId){
        log.info("接口名称：动态-取消喜欢");
        movementsMQService.disLovePublishMsg(publishId);
        return commentService.unloveComment(publishId);
    }

    /**
     * 接口名称：单条动态
     * 接口路径：GET/movements/:id
     * 需求分析：根据动态id查询，并返回MovementsVo对象
     */
    @GetMapping("{id}")
    public ResponseEntity<Object> queryMovementsById(@PathVariable("id") String publishId){
        log.info("接口名称：单条动态");
        return commentService.queryMovementsById(publishId);
    }

    /**
     * 接口名称：用户动态
     * 接口路径：GET/movements/all
     */
    @GetMapping("/all")
    public ResponseEntity<Object> queryAlbumPublishList(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer pagesize,Long userId){
        if(page<1){
            page=1;
        }
        return movementsService.queryAlbumPublishList(page,pagesize);
    }

    /**
     * 接口名称：谁看过我
     * 接口路径：GET/movements/visitors
     */
    @GetMapping("visitors")
    public ResponseEntity<Object> queryVisitors(){
        return movementsService.queryVisitors();
    }


}
